package com.shengde.jinwawaforleader.model.homePage;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProAttendanceRecoder;
import com.shengde.jinwawaforleader.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 打卡adapter
 * Created by Administrator on 2016/9/2 0002.
 */
public class DakaRecodeAdapter extends BaseAdapter {
	private static final int ITEM_TEACHER_TITLE = 1;
	private static final int ITEM_TEACHER = 2;
	private static final int ITEM_STUDENT_TITLE = 3;
	private static final int ITEM_STUDENT = 4;
	private static final String TAG = "DakaRecodeAdapter";
	private List<ProAttendanceRecoder.StuBean> student_list;
	private List<ProAttendanceRecoder.TeacherBean> teacher_list;
	private Context context;

	viewHolder_seekbar holder_seekbar = null;
	viewHolder_button holder_button = null;

	public DakaRecodeAdapter(Context context, List<ProAttendanceRecoder.StuBean> student_list, List<ProAttendanceRecoder.TeacherBean> teacher_list) {
		this.context = context;
		this.student_list = student_list;
		this.teacher_list = teacher_list;
		Log.i(TAG, "DakaRecodeAdapter: studnet" + student_list.size());
		Log.i(TAG, "DakaRecodeAdapter: teacher" + teacher_list.size());
	}

	@Override
	public int getCount() {
		int count = 0;
//		这里的+1 是为了显示表头
		if (teacher_list != null) {
			count += teacher_list.size() + 1;
		}
		if (student_list != null) {
			count += student_list.size() + 1;
		}
		return count;
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {


		int type = getItemViewType(position);
		if (type == ITEM_TEACHER_TITLE) {
			convertView = View.inflate(context, R.layout.teacher_attendance, null);
			return convertView;
		}
		if (type == ITEM_STUDENT_TITLE) {
			convertView = View.inflate(context, R.layout.student_attendance, null);
			return convertView;
		}
		if (convertView == null) {
			switch (type) {
				case ITEM_TEACHER:
					convertView = LayoutInflater.from(context).inflate(R.layout.teacher_attendance_item, parent, false);

					holder_seekbar = new viewHolder_seekbar();
					holder_seekbar.iv_person_logo = (ImageView) convertView.findViewById(R.id.iv_person_logo);
					holder_seekbar.tv_teacher_name = (TextView) convertView.findViewById(R.id.tv_teacher_name);
					holder_seekbar.tv_daka = (TextView) convertView.findViewById(R.id.tv_daka);
					holder_seekbar.tv_come_time = (TextView) convertView.findViewById(R.id.tv_come_time);
					holder_seekbar.tv_go_time = (TextView) convertView.findViewById(R.id.tv_go_time);

					convertView.setTag(holder_seekbar);
					break;
				case ITEM_STUDENT:
					holder_button = new viewHolder_button();
					convertView = LayoutInflater.from(context).inflate(R.layout.student_attendance_item, parent, false);
					holder_button.iv_person_logo = (ImageView) convertView.findViewById(R.id.iv_person_logo);
					holder_button.tv_student_name = (TextView) convertView.findViewById(R.id.tv_student_name);
					holder_button.tv_daka = (TextView) convertView.findViewById(R.id.tv_daka);
					holder_button.in_graden_time = (TextView) convertView.findViewById(R.id.in_graden_time);
					holder_button.out_graden_time = (TextView) convertView.findViewById(R.id.out_graden_time);
					convertView.setTag(holder_button);
					break;
			}
		} else {//有convertView，按样式，取得不用的布局
			switch (type) {
				case ITEM_TEACHER:
					holder_seekbar = (viewHolder_seekbar) convertView.getTag();
					Log.e("convertView !!!!!!= ", "NULL TYPE_SEEKBAR");
					break;
				case ITEM_STUDENT:
					holder_button = (viewHolder_button) convertView.getTag();
					Log.e("convertView !!!!!!= ", "NULL TYPE_BUTTON");
					break;
			}
		}
		//需求： 先显示老师考勤，然后再显示学生考勤
		ProAttendanceRecoder.TeacherBean teacherInfo = null;
		ProAttendanceRecoder.StuBean studentInfo = null;

		if (position < teacher_list.size() + 1) {  //如果是老师考勤条件判断满足
			teacherInfo = teacher_list.get(position - 1);

			if ("".equals(teacherInfo.getLogo()) || teacherInfo.getLogo() == null) {
				holder_seekbar.iv_person_logo.setImageResource(R.drawable.touxiang);
			} else {

				Glide.with(context).load(BaseProtocol.IMG_BASE + teacherInfo.getLogo())
						.error(R.drawable.touxiang)
						.into(holder_seekbar.iv_person_logo);
			}

			holder_seekbar.tv_teacher_name.setText(teacherInfo.getName());
			if ("1".equals(teacherInfo.getFlag()) || teacherInfo.getFlag() == 1) {
				holder_seekbar.tv_daka.setText(context.getString(R.string.finsh_da_ka));
				holder_seekbar.tv_daka.setTextColor(context.getResources().getColor(R.color.upgrade_green));
				if (teacherInfo.getTime3() == null || "".equals(teacherInfo.getTime3())) {
					holder_seekbar.tv_come_time.setText("未入园");
				} else {
					holder_seekbar.tv_come_time.setText(DateUtil.getDateT(teacherInfo.getTime3()));

				}
				if (teacherInfo.getTime4() == null || "".equals(teacherInfo.getTime4())) {
					holder_seekbar.tv_go_time.setText("未出园");
				} else {
					holder_seekbar.tv_go_time.setText(DateUtil.getDateT(teacherInfo.getTime4()));
				}
			} else {
				holder_seekbar.tv_daka.setText(context.getString(R.string.unfinsh_da_ka));
				holder_seekbar.tv_daka.setTextColor(context.getResources().getColor(R.color.upgrade_red));
				holder_seekbar.tv_come_time.setText("未入园");
				holder_seekbar.tv_go_time.setText("未出园");

			}

		} else {

			//注意： 由于前面显示过了教师考勤，所以这里的position务必要减去早前的考勤大小，否则将会出现角标越界
			studentInfo = student_list.get(position - teacher_list.size() - 2);

			if ("".equals(studentInfo.getLogo()) || studentInfo.getLogo() == null) {
				holder_button.iv_person_logo.setImageResource(R.drawable.touxiang);
			} else {
				Glide.with(context)
						.load(BaseProtocol.IMG_BASE + studentInfo.getLogo())
						.error(R.drawable.touxiang)
						.into(holder_button.iv_person_logo);
			}

			holder_button.tv_student_name.setText(studentInfo.getName());

			Log.i(TAG, "getView: stu_flag" + studentInfo.getFlag());
			if ("1".equals(studentInfo.getFlag()) || studentInfo.getFlag() == 1) {
				holder_button.tv_daka.setText(context.getString(R.string.finsh_da_ka));
				holder_button.tv_daka.setTextColor(context.getResources().getColor(R.color.upgrade_green));
				if (studentInfo.getTime3() == null || "".equals(studentInfo.getTime3())) {
					holder_button.in_graden_time.setText("未入园");
				} else {
					holder_button.in_graden_time.setText(DateUtil.getDateT(studentInfo.getTime3()));

				}
				if (studentInfo.getTime4() == null || "".equals(studentInfo.getTime4())) {
					holder_button.out_graden_time.setText("未出园");
				} else {
					holder_button.out_graden_time.setText(DateUtil.getDateT(studentInfo.getTime4()));
				}

			} else {
				holder_button.tv_daka.setText(context.getString(R.string.unfinsh_da_ka));
				holder_button.tv_daka.setTextColor(context.getResources().getColor(R.color.upgrade_red));
				holder_button.in_graden_time.setText("未入园");
				holder_button.out_graden_time.setText("未出园");

			}

		}

		return convertView;
	}


	@Override
	public int getItemViewType(int position) {
		if (position == 0) {
			return ITEM_TEACHER_TITLE;

		}
		if (position < teacher_list.size() + 1) {
			return ITEM_TEACHER;
		}

		if (position == teacher_list.size() + 1) {
			return ITEM_STUDENT_TITLE;
		}
		if (position <= teacher_list.size() + student_list.size() + 2) {
			return ITEM_STUDENT;

		}
		return super.getItemViewType(position);
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public int getViewTypeCount() {
		return 5;
	}

	class viewHolder_seekbar {
		ImageView iv_person_logo; //老师头像
		TextView tv_teacher_name;  //老师名字
		TextView tv_daka;     // 0未打卡  1已打卡
		TextView tv_come_time;  //入园时间
		TextView tv_go_time;  //出园时间

	}

	class viewHolder_button {
		ImageView iv_person_logo; //老师头像
		TextView tv_student_name;  //老师名字
		TextView tv_daka;     // 0未打卡  1已打卡
		TextView in_graden_time;     // 入园时间
		TextView out_graden_time;     //出园时间

	}
}
